#define  _CRT_SECURE_NO_WARNINGS

#include <cstdio>

#define GLFW_DLL
#include <gl\glfw.h>
#include <gl\glu.h>							// Header File For The GLu32 Library

#include "..\logger\logger.h"				// Logger class
#include "net.h"							// Net class


const int RESX = 800;
const int RESY = 600;

Logger* log;

int main()
{
	log = new Logger("logfile.txt");

	if(glfwInit() == GL_FALSE)
		log->Error("glfwInit failed\n", Logger::CRITICAL);

	glfwOpenWindowHint(GLFW_WINDOW_NO_RESIZE, GL_TRUE);

	if(glfwOpenWindow(RESX, RESY, 0, 0, 0, 0, 0, 0, GLFW_WINDOW) == GL_FALSE)
		log->Error("glfwOpenWindow failed\n", Logger::CRITICAL);

	glfwEnable(GLFW_STICKY_KEYS);

	glMatrixMode(GL_PROJECTION);
	glOrtho(0, 1, 1, 0, 0, 1.f);
	glMatrixMode(GL_MODELVIEW);
	glDisable(GL_DEPTH_TEST);
	glLoadIdentity();

	glfwSwapInterval(1);
	glClear(GL_COLOR_BUFFER_BIT);
	glEnable(GL_TEXTURE_2D);

	// Loading textures.
	GLFWimage *gui = new GLFWimage, *sprite = new GLFWimage;

	glfwReadImage("gfx\\gui.tga", gui, GLFW_ORIGIN_UL_BIT | GLFW_BUILD_MIPMAPS_BIT);
	glfwReadImage("gfx\\NeHe.tga", sprite, GLFW_ORIGIN_UL_BIT | GLFW_BUILD_MIPMAPS_BIT);

	// Setting up scissor box.
	glScissor(0, 0, 700, 600);

	int running = GL_TRUE;
	double time = glfwGetTime(), newtime;
	unsigned FPS;

	while(running)
	{
		running = glfwGetWindowParam(GLFW_OPENED) && !glfwGetKey(GLFW_KEY_ESC);

		
		// Drawning GUI and setting the scissor box.
		glfwLoadTextureImage2D(gui, GLFW_ORIGIN_UL_BIT | GLFW_BUILD_MIPMAPS_BIT);

		glDisable(GL_SCISSOR_TEST);

		glPushMatrix();
		glLoadIdentity();
		glBegin(GL_QUADS);
		glTexCoord2f(0, 0);
		glVertex2f(700.f / RESX, 0);
		glTexCoord2f(1.f, 0);
		glVertex2f(1.f, 0);
		glTexCoord2f(1.f, 1.f);
		glVertex2f(1.f, 1.f);
		glTexCoord2f(0, 1.f);
		glVertex2f(700.f / RESX, 1.f);
		glEnd();

		glEnable(GL_SCISSOR_TEST);

		// Drawning the sprite.
		glClear(GL_COLOR_BUFFER_BIT);
		glfwLoadTextureImage2D(sprite, GLFW_ORIGIN_UL_BIT | GLFW_BUILD_MIPMAPS_BIT);

		
		glPopMatrix();
		if(glfwGetKey(GLFW_KEY_RIGHT))
			glTranslatef(0.01f, 0, 0);
		if(glfwGetKey(GLFW_KEY_LEFT))
			glTranslatef(-0.01f, 0, 0);
		if(glfwGetKey(GLFW_KEY_UP))
			glTranslatef(0, -0.01f, 0);
		if(glfwGetKey(GLFW_KEY_DOWN))
			glTranslatef(0, 0.01f, 0);

		glBegin(GL_QUADS);
		glTexCoord2f(0.f, 0.f);
		glVertex2f(0.f, 0.f);
		glTexCoord2f(1.f, 0.f);
		glVertex2f(0.5f, 0.f);
		glTexCoord2f(1.f, 1.f);
		glVertex2f(0.5f, 0.5f);
		glTexCoord2f(0.f, 1.f);
		glVertex2f(0.f, 0.5f);
		glEnd();

		glfwSwapBuffers();


		newtime = glfwGetTime();
		FPS = 1.f/(newtime - time);
		time = newtime;
		printf("FPS: %u\n", FPS);
	}

	glfwFreeImage(gui);
	glfwFreeImage(sprite);
	glfwTerminate();
	delete log;
}


//// File used for tests. It's a mess so don't read it.

// Net tests.
//#include <cstdio>
//#include <cstring>
//
//#include "..\logger\logger.h"
//#include "net.h"
//
//
//
//Logger* log;
//int main()
//{
//	log = new Logger("logfile.txt");
//
//	Net sock;
//	sock.Connect("google.com", "80");
//
//	const char* request = "GET http://www.google.com/ HTTP/1.1\n" "Host: http://www.google.com/\n\n";
//	sock.Send(request, strlen(request));
//
//	char buffer[0x1000];
//	sock.Recv(buffer, 0x1000);
//	
//	FILE* file = fopen("recv.dat", "w");
//	fwrite(buffer, 1, 0x1000, file);
//	fclose(file);
//	
//}




